package com.example.music_liwei.activity;

import androidx.appcompat.app.AppCompatActivity;
import androidx.core.view.GravityCompat;
import androidx.drawerlayout.widget.DrawerLayout;
import androidx.fragment.app.FragmentManager;
import androidx.fragment.app.FragmentTransaction;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import android.content.Intent;
import android.os.Bundle;
import android.view.Gravity;
import android.view.View;
import android.widget.TextView;

import com.example.music_liwei.R;
import com.example.music_liwei.adapter.MusicListAdapter;
import com.example.music_liwei.bean.ResponseData;
import com.example.music_liwei.fragment.BlankFragment1;
import com.example.music_liwei.fragment.BlankFragment2;
import com.example.music_liwei.fragment.BlankFragment3;
import com.example.music_liwei.fragment.BlankFragment4;
import com.example.music_liwei.fragment.BlankFragmentShowAll;
import com.example.music_liwei.messageEvent.UpdateDrawerEvent;
import com.google.android.material.navigation.NavigationView;
import com.scwang.smart.refresh.footer.ClassicsFooter;
import com.scwang.smart.refresh.header.ClassicsHeader;
import com.scwang.smart.refresh.layout.api.RefreshLayout;
import com.scwang.smart.refresh.layout.listener.OnLoadMoreListener;
import com.scwang.smart.refresh.layout.listener.OnRefreshListener;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import java.util.ArrayList;
import java.util.List;

public class MusicMainActivity extends AppCompatActivity implements BlankFragmentShowAll.OnMenuIconClickListener {

    private DrawerLayout drawerLayout;
    private RecyclerView rvMusicList;
    private MusicListAdapter musicListAdapter;
    private List<ResponseData.MusicInfo> musicInfoList;
    private TextView tvMusicCount, tvPlayMode;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_music_main);

        drawerLayout = findViewById(R.id.drawer_layout);

        FragmentManager fragmentManager = getSupportFragmentManager();
        FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
        fragmentTransaction.add(R.id.fl_show1, new BlankFragment1(),"BlankFragment1");
        fragmentTransaction.commit();

        FragmentManager fragmentManager1 = getSupportFragmentManager();
        FragmentTransaction fragmentTransaction1 = fragmentManager1.beginTransaction();
        fragmentTransaction1.add(R.id.fl_show2, new BlankFragment2(),"BlankFragment2");
        fragmentTransaction1.commit();

        FragmentManager fragmentManager2 = getSupportFragmentManager();
        FragmentTransaction fragmentTransaction2 = fragmentManager2.beginTransaction();
        fragmentTransaction2.add(R.id.fl_show3, new BlankFragment3(),"BlankFragment3");
        fragmentTransaction2.commit();

        FragmentManager fragmentManager3 = getSupportFragmentManager();
        FragmentTransaction fragmentTransaction3 = fragmentManager3.beginTransaction();
        fragmentTransaction3.add(R.id.fl_show4, new BlankFragment4(),"BlankFragment4");
        fragmentTransaction3.commit();

        FragmentManager fragmentManager4 = getSupportFragmentManager();
        FragmentTransaction fragmentTransaction4 = fragmentManager4.beginTransaction();
        fragmentTransaction4.add(R.id.fl_show_all,new BlankFragmentShowAll(),"BlankFragmentShowAll");
        fragmentTransaction4.commit();

        // SmartRefreshLayout 上拉加载，下拉刷新
        RefreshLayout refreshLayout = findViewById(R.id.refreshLayout);
        refreshLayout.setRefreshHeader(new ClassicsHeader(this));
        refreshLayout.setRefreshFooter(new ClassicsFooter(this));
        refreshLayout.setOnRefreshListener(refreshlayout -> refreshlayout.finishRefresh(2000/*,false*/)); //传入false表示刷新失败
        refreshLayout.setOnLoadMoreListener(refreshlayout -> refreshlayout.finishLoadMore(2000/*,false*/)); //传入false表示加载失败

        // 初始化抽屉中的RecyclerView
        NavigationView navView = findViewById(R.id.nav_view);
        View headerView = navView.getHeaderView(0);
        rvMusicList = headerView.findViewById(R.id.rv_music_list);
        rvMusicList.setLayoutManager(new LinearLayoutManager(this));
        musicInfoList = new ArrayList<>();
        musicListAdapter = new MusicListAdapter(musicInfoList, position -> {
            musicInfoList.remove(position);
            updateMusicList();
        });
        rvMusicList.setAdapter(musicListAdapter);

        tvMusicCount = headerView.findViewById(R.id.tv_music_count);
        tvPlayMode = headerView.findViewById(R.id.tv_play_mode);

        EventBus.getDefault().register(this); // 注册EventBus
    }

    @Override
    public void onMenuIconClick() {
        drawerLayout.openDrawer(GravityCompat.END);
    }

    public void onSwipeUp() {
        Intent intent = new Intent(MusicMainActivity.this, MusicPlayerActivity.class);
        startActivity(intent);
        overridePendingTransition(R.anim.slide_up, R.anim.no_change);
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void onUpdateDrawerEvent(UpdateDrawerEvent event) {
        musicInfoList.add(event.getMusicInfo());
        updateMusicList();
    }

    private void updateMusicList() {
        tvMusicCount.setText("当前播放: " + musicInfoList.size() + "首");
        musicListAdapter.updateMusicList(musicInfoList);
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        EventBus.getDefault().unregister(this); // 注销EventBus
    }
}
